package com.example.business;

import com.example.data.mapper.ArticleMapper;
import com.example.data.model.Article;
import com.example.page.Page;
import com.example.page.Pageable;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Created by hldev on 16-12-1.
 */
@Service
public class ArticleService {

    @Autowired
    private final ArticleMapper articleMapper;

    @Autowired
    private final UserService userService;

    public ArticleService(ArticleMapper articleMapper, UserService userService) {
        this.articleMapper = articleMapper;
        this.userService = userService;
    }


    public Optional<Article> findById(Integer id) {
        return Optional.ofNullable(articleMapper.findById(id))
                .map(article -> {
                    article.setContent(clearText(article.getContent()));
                    article.setBrowseCount(article.getBrowseCount() + 1);
                    updateArticle(article);
                    return article;
                });
    }

    public List<Article> findAll() {
        return articleMapper.findAll()
                .stream()
                .map(article -> {
                    article.setContent(clearText(article.getContent()));
                    return article;
                })
                .collect(Collectors.toList());
    }

    public Page<Article> findPage(Integer category, String title, Pageable pageable, Integer status, String beginDate, String endDate) {
        pageable.addSomeParam("category", category)
                .addSomeParam("title", title)
                .addSomeParam("beginDate", beginDate)
                .addSomeParam("endDate", endDate)
                .addSomeParam("status", status);

        List<Article> list = articleMapper.findPage(pageable)
                .stream()
                .map(article -> {
                    article.setContent(clearText(article.getContent()));
                    return article;
                })
                .collect(Collectors.toList());
        return new Page<>(pageable, list);
    }

    public void createArticle(Article article) {
//        Optional<AuthToken> authToken = AuthToken.getFromThreadLocal();
//        String x = AuthToken.getFromThreadLocal().get().userName;
//        article.setCreateBy(x);
        article.setBrowseCount(0);
//        article.setCreateDate(LocalDateTime.now());
//        String orgId = userService.findById(AuthToken.getFromThreadLocal().get().ownerId).get().getOrgId();
//        article.setOrgId(orgId);
        articleMapper.create(article);
    }

    @Transactional
    public void updateArticle(Article article) {
        articleMapper.update(article);
    }

    public void deleteArticle(Integer id) {
        articleMapper.delete(id);
    }

    private String clearText(String content) {
        if (StringUtils.isBlank(content))
            return "";

        return content.replaceAll("\\\\r\\\\n", "");
    }
}
